import numpy as np
from sklearn import linear_model
import sklearn.metrics as sm
import matplotlib.pyplot as plt

input = r'/Users/qin/Documents/workspaces/office_one/machine_learning_with_python_tutorialspoint/Regression/15.Overview/coffee.csv'
input_data = np.loadtxt(input, delimiter=',', skiprows=1)
# 打乱数据
np.random.shuffle(input_data)
# print(input_data)
X, y = input_data[:, 1:-1], input_data[:, -1]
# print(X)
# print(y)

# As we need to test out model on unseen data hence, we will divide our dataset into two parts: a training set and a test set.
num_training = int(0.6 * len(X))
num_testing = len(X) - num_training
X_train, y_train = X[:num_training], y[:num_training]
X_test, y_test = X[num_training:], y[num_training:]

# train this model with the training samples
reg_linear = linear_model.LinearRegression()
reg_linear.fit(X_train, y_train)

# do the prediction with the testing data
y_test_pred = reg_linear.predict(X_test)

# visualize
plt.scatter(X_test, y_test, color='red')
plt.plot(X_test, y_test_pred, color='black', linewidth=2)
plt.xticks(())
plt.yticks(())
plt.show()